document.addEventListener('DOMContentLoaded', () => {
    const deleteButton = document.getElementById('deleteButton');
    const statusDiv = document.getElementById('status');
    const form = document.querySelector('.container');

    deleteButton.addEventListener('click', async () => {
        const accessKeyId = document.getElementById('accessKeyId').value;
        const accessKeySecret = document.getElementById('accessKeySecret').value;
        const region = document.getElementById('region').value;
        const bucket = document.getElementById('bucket').value;

        if (!accessKeyId || !accessKeySecret || !region || !bucket) {
            showStatus('请填写所有必填字段', 'error');
            return;
        }

        deleteButton.disabled = true;
        showStatus('正在删除文件...', '');

        try {
            const response = await fetch('/api/delete', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    accessKeyId,
                    accessKeySecret,
                    region,
                    bucket
                })
            });

            const data = await response.json();

            if (response.ok) {
                showStatus(`成功删除 ${data.deletedCount} 个文件`, 'success');
            } else {
                showStatus(`错误: ${data.message}`, 'error');
            }
        } catch (error) {
            showStatus('发生错误: ' + error.message, 'error');
        } finally {
            deleteButton.disabled = false;
        }
    });

    function showStatus(message, type) {
        statusDiv.textContent = message;
        statusDiv.className = 'status ' + type;
    }
}); 